﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace StockControl
{
    public delegate void StockListViewPaintItem(ListViewItem item, object data);

    public class StockListView : ListView
    {
        public StockListViewPaintItem PaintItem { get; set; }

        private IEnumerable _dataSource = null;

        public IEnumerable DataSource
        {
            set
            {
                _dataSource = value;

                Init();
            }
            get
            {
                return this._dataSource;
            }
        }

        public StockListView()
            : base()
        {
        }

        private void Init()
        {
            if (PaintItem == null)
                return;

            if (DataSource == null)
                return;

            this.Items.Clear();

            int i = 0;
            foreach (object data in DataSource)
            {
                ListViewItem item = new ListViewItem();
                this.Items.Insert(i, item);
                PaintItem(item, data);
                i++;
            }

            if (this.SelectedItems.Count <= 0 && this.Items.Count > 0)
            {
                this.Items[0].Selected = true;
            }
        }

        public void Bind()
        {
            if (PaintItem == null)
                return;

            if (DataSource == null)
                return;

            bool reselectItem = false;

            // 删除多余的项
            foreach (ListViewItem lvi in this.Items)
            {
                object itemData = null;
                foreach (object data in DataSource)
                {
                    if (lvi.Tag == data)
                    {
                        itemData = data;
                        break;
                    }
                }
                if (itemData == null)
                {
                    foreach (ListViewItem slvi in this.SelectedItems)
                    {
                        if (slvi.Tag == lvi.Tag)
                        {
                            reselectItem = true;
                            break;
                        }
                    }

                    this.Items.Remove(lvi);
                }
                else
                {
                    PaintItem(lvi, itemData);
                }
            }

            // 添加不存在的项
            int i = 0;
            foreach (object data in DataSource)
            {
                bool exist = false;
                foreach (ListViewItem lvi in this.Items)
                {
                    if (lvi.Tag == data)
                    {
                        exist = true;
                        break;
                    }
                }
                if (!exist)
                {
                    ListViewItem item = new ListViewItem();
                    this.Items.Insert(i, item);
                    PaintItem(item, data);
                }

                i++;
            }

            // 如果顺序不合适，调整顺序
            i = 0;
            foreach (object data in DataSource)
            {
                // 找到标签
                int j = 0;
                for (j = 0; j < this.Items.Count; j++)
                {
                    if (this.Items[j].Tag == data)
                    {
                        break;
                    }
                }

                if (j != i)
                {
                    //currentItem = this.SelectedItems[0].Tag;
                    ListViewItem itemj = this.Items[j];
                    this.Items.RemoveAt(j);
                    this.Items.Insert(i, itemj);
                    //tabReport.SelectedTab = pageSelect;
                }

                i++;
            }

            if (reselectItem)
            {
                //BlockTabSelectFirst();
            }

            if (this.SelectedItems.Count <= 0 && this.Items.Count > 0)
            {
                this.Items[0].Selected = true;
            }
        }
    }
}
